package com.ruoyi.project.user.host.controller;

import com.ruoyi.common.constant.SqlOrder;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.post.domain.Post;
import com.ruoyi.project.user.host.domain.Host;
import com.ruoyi.project.user.host.service.HostService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * Host控制器
 */
@Controller
@RequestMapping("/user/host")
public class HostController extends BaseController {
    private String prefix = "user/host";

    @Autowired
    private HostService hostService;


    @RequiresPermissions("user:host:view")
    @GetMapping("")
    public String showView() {
        return prefix + "/host";
    }

    /**
     * 显示页面数据
     *
     * @return
     */
    @RequiresPermissions("user:host:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo demo(Host host, int hostOrderNum) {
        startPage(SqlOrder.getSqlOrder(hostOrderNum));
        List<Host> list = hostService.showHost(host);

        return getDataTable(list);
    }

    @RequiresPermissions("user:host:add")
    @GetMapping("/add")
    public String showAdd() {
        return prefix + "/add";
    }

    /**
     * 检查手机号是否存在
     *
     * @param hostPhone
     * @return
     */
    @RequiresPermissions("user:host:add")
    @PostMapping("/checkPhone")
    @ResponseBody
    public Long checkPhone(String hostPhone) {
        return hostService.checkPhone(hostPhone);
    }

    /**
     * 检查邮箱是否存在
     *
     * @param hostEmail
     * @return
     */
    @RequiresPermissions("user:host:add")
    @PostMapping("/checkEmail")
    @ResponseBody
    public Long checkEmail(String hostEmail) {
        return hostService.checkEmail(hostEmail);
    }

    /**
     * 新增数据
     *
     * @param host
     * @param hostPassword
     * @return
     */
    @RequiresPermissions("user:host:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(Host host, String hostPassword) {
        return toAjax(hostService.insert(host, hostPassword));
    }

    @RequiresPermissions("user:host:sortweight")
    @PostMapping("/update/sortweight")
    @ResponseBody
    public AjaxResult updateSortWeight(Short hostSortWeight, Long hostId) {
        return toAjax(hostService.updateSortweightByHostId(hostSortWeight, hostId));
    }

    @RequiresPermissions("user:host:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Host host, int hostOrderNum) {
        List<Host> list = hostService.export(host, hostOrderNum);
        ExcelUtil<Host> util = new ExcelUtil<Host>(Host.class);
        return util.exportExcel(list, "主持人数据");
    }

    /**
     * 多条删除
     * @param ids
     * @return
     */
    @RequiresPermissions("user:host:remove")
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(Long[] ids) {
        return toAjax(hostService.delete(ids));
    }

    @RequiresPermissions("user:host:edit")
    @GetMapping("/edit/{hostId}")
    public String showEdit(@PathVariable Long hostId, Model model){
        model.addAttribute("host",hostService.showHostById(hostId));
        return prefix+"/edit";
    }
    @RequiresPermissions("user:host:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult edit(Host host){
        return toAjax(hostService.updateHost(host));
    }


    @RequiresPermissions("user:host:state")
    @PostMapping("/updateState")
    @ResponseBody
    public AjaxResult updateState(Host host){
        return toAjax(hostService.updateState(host));
    }

    @RequiresPermissions("user:host:permissionset")
    @GetMapping("/permissionset/{hostId}")
    public String permissionset(@PathVariable Long hostId, Model model){
        System.out.println(hostService.showHostById(hostId));
        model.addAttribute("host",hostService.showHostById(hostId));
        return prefix+"/permissionset";
    }
    @RequiresPermissions("user:host:permissionset")
    @PostMapping("/perset")
    @ResponseBody
    public AjaxResult updatePermissionset(Host host){
        return toAjax(hostService.updatePermissionsetByHostId(host));
    }
}
